package com.cn.medicalcare.controller.backstageservlet.login;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.Admin;
import com.cn.medicalcare.service.LoginService;
import com.cn.medicalcare.service.serviceimpl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/login.json")
public class LoginServlet extends HttpServlet {

    private LoginService loginService = LoginServiceImpl.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        PrintWriter pw = resp.getWriter();
        Map<String, Object> map = new LinkedHashMap<>();
        if ((username == null || username.length() == 0) || (password == null || password.length() < 5)) {
            //用户名或密码格式错误,返回400
            map.put("status", 400);
            pw.print(JsonTool.toJson(map));
        } else {
            map = loginService.isLogin(username, password);
            pw.print(JsonTool.toJson(map));
        }
    }
}

